/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-06-07     洪家团       the first version
 */

#include <board.h>
#include <rtthread.h>
#include "light.h"

#define ADC_DEV_NAME        "adc1"      /* ADC 设备名称 */
#define ADC_DEV_CHANNEL1     0           /* ADC 通道0 */
#define ADC_DEV_CHANNEL2     1           /* ADC 通道1 */
#define ADC_DEV_CHANNEL3     2           /* ADC 通道2 */


//声明外部变量
extern unsigned int g_light_value1;
extern unsigned int g_light_value2;
extern unsigned int g_light_value3;

//定义ADC设备句柄
rt_adc_device_t adc_dev;

/**
 * @brief    该函数用于初始化光敏传感器
 * @note     设置其为12位精度,使能了adc设备，以及通道0 1 2
 * @param    无传入的参数
 * @return   无返回值
 */
void light_init(void)
{

    rt_err_t ret = RT_EOK;

    /*使能ADC设备*/
    adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
    if (adc_dev == RT_NULL)
    {
        rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
    }
    /* 使能通道1 */
    ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL1);
    if(ret == RT_EOK)
    {
        rt_kprintf("adc sample run success!  find %s device!\n", ADC_DEV_NAME);
    }

    /* 使能通道2 */
    ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL2);
    if(ret == RT_EOK)
    {
        rt_kprintf("adc sample run success!  find %s device!\n", ADC_DEV_NAME);
    }

    /* 使能通道3 */
    ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL3);
    if(ret == RT_EOK)
    {
        rt_kprintf("adc sample run success!  find %s device!\n", ADC_DEV_NAME);
    }
}

/**
 * @brief    该函数用于获取光敏传感器值1
 * @note     范围为12位，即0-4095
 * @param    无传入的参数
 * @return   无
 */
void get_light_value1(void)
{
    /* 读取采样值 */
    g_light_value1 = rt_adc_read(adc_dev, ADC_DEV_CHANNEL1);
    rt_kprintf("the g_light_value1 is :%d \n", g_light_value1);
}

/**
 * @brief    该函数用于获取光敏传感器值2
 * @note     范围为12位，即0-4095
 * @param    无传入的参数
 * @return   无
 */
void get_light_value2(void)
{
    /* 读取采样值 */
    g_light_value2 = rt_adc_read(adc_dev, ADC_DEV_CHANNEL2);
    rt_kprintf("the g_light_value2 is :%d \n", g_light_value2);
}

/**
 * @brief    该函数用于获取光敏传感器值3
 * @note     范围为12位，即0-4095
 * @param    无传入的参数
 * @return   无
 */
void get_light_value3(void)
{
    /* 读取采样值 */
    g_light_value3 = rt_adc_read(adc_dev, ADC_DEV_CHANNEL3);
    rt_kprintf("the g_light_value3 is :%d \n", g_light_value3);
}
